const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const Ymd = new Date()
exports.main = async(event, context) => {
  try {
    if (event.ktzt == 0) {
      const a = await db.collection('Option').where({
        topicId: event._id
      }).field({
        userId: true
      }).get()
      for (var i = 0; i < a.data.length; i++) {
        await db.collection('Message').add({
          data: {
            sendId: event.sendId,
            sendName: event.sendName,
            receiveId: a.data[i].userId,
            messageType: '课题删除通知',
            topicId: event._id,
            topicName: event.ktmc,
            time: Date.parse(new Date()),
            dateYmd: Ymd.getFullYear() + '/' + (Ymd.getMonth() + 1) + '/' + Ymd.getDate()
          }
        })
      }
    }
    await db.collection('Topic').doc(event._id).remove()
    await db.collection('Option').where({
      topicId: event._id
    }).remove()
    return {
      result: 1
    }
  } catch (e) {
    return {
      result: 0
    }
  }
}